<?php

require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_CONFIG . 'db_connection.php';

class dao_paper_category {

    //Methods
    public function __construct() {
        
    }

    public function get_all() {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_paper_category";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }

        $db->close_connect($con);
        return $list;
    }

    public function add($id, $cat_name) {
        $db = new db_connection();
        $con = $db->open_connect();

        if($id == 0){
        $query = "INSERT INTO tbl_paper_category(cat_name) VALUES ('" . $cat_name . "')";}
        else {
            $query = "UPDATE tbl_paper_category SET cat_name = '" . $cat_name . "' WHERE cat_id = " . $id;
        }
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect($con);
        return true;
    }

    public function get_by_id($cat_id) {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_paper_category WHERE cat_id = " . $cat_id;
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect($con);
        return $row;
    }

    public function delete($cat_id) {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "DELETE FROM tbl_paper_category WHERE cat_id = " . $cat_id;
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect($con);
        return true;
    }

	public function get_name_by_id($id) {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "SELECT cat_name FROM tbl_paper_category WHERE cat_id = " . $id;
        $result = mysqli_query($con, $query);
        $row = mysqli_fetch_array($result);

        $db->close_connect($con);
        return $row['cat_name'];
    }
    
	
}
